package com.system.borrow.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.AdapterView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.system.borrow.Constant.URLDefind;
import com.system.borrow.R;
import com.system.borrow.adapter.TuiJianAdapter;
import com.system.borrow.bean.FastBean;
import com.system.borrow.bean.GloData;
import com.system.borrow.bean.TuiJian;
import com.system.borrow.cache.CacheMode;
import com.system.borrow.callback.DialogCallback;
import com.system.borrow.http.utils.OkHttpUtils;
import com.system.borrow.pull.XListView;
import com.system.borrow.utils.SecondTitle;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Request;
import okhttp3.Response;

public class FastDetailActivity extends BaseActivity implements XListView.IXListViewListener {
    XListView lv_data;
    TuiJianAdapter adapter;
    FastBean fastBean;
    List<TuiJian> datas = new ArrayList<>();
    int pageNo = 1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fast_detail);
        fastBean = (FastBean) getIntent().getSerializableExtra("fast");
        SecondTitle title = new SecondTitle(this);
        title.setTitle(fastBean.name,null);
        initView();
        addAction();
        initListener();
        loadData(fastBean.id);
    }

    public void loadData(int id) {
        OkHttpUtils.get(URLDefind.LOAN_LIST_BY_TAG)
                .tag(this)
                .cacheMode(CacheMode.FIRST_CACHE_THEN_REQUEST)
                .params("categoryId",id+"")
                .params("pageNo",pageNo+"")
                .params("pageSize","10")
                .execute(new DialogCallback<String>(this,String.class,"加载中...") {
                    @Override
                    public void onResponse(boolean isFromCache, String s, Request request, @Nullable Response response) {
                        lv_data.stopLoadMore();
                        lv_data.stopRefresh();
                        if(s!=null){
                            try {
                                JSONObject json = new JSONObject(s);
                                int statusCode = json.getInt("statusCode");
                                if(statusCode==200){
                                    if(pageNo==1&&datas.size()>0){
                                        datas = new ArrayList<TuiJian>();
                                    }
                                    JSONArray data = json.getJSONArray("data");
                                    Gson gson = new Gson();
                                    TypeToken<List<TuiJian>> token = new TypeToken<List<TuiJian>>(){};
                                    List<TuiJian> tuiJianList = gson.fromJson(data.toString(),token.getType());
                                    if(tuiJianList==null || tuiJianList.size()==0){
                                        ToastShow("暂无更多数据");
                                    }else{
                                        datas.addAll(tuiJianList);
                                        adapter.notifyDataSetChanged();
                                    }
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    }

                    @Override
                    public void onCacheResponse(boolean isFromCache, String s, Request request, @Nullable Response response) {
                        if(s!=null){
                            try {
                                JSONObject json = new JSONObject(s);
                                int statusCode = json.getInt("statusCode");
                                if(statusCode==200 && pageNo==1){
                                    JSONArray data = json.getJSONArray("data");
                                    Gson gson = new Gson();
                                    TypeToken<List<TuiJian>> token = new TypeToken<List<TuiJian>>(){};
                                    List<TuiJian> tuiJianList = gson.fromJson(data.toString(),token.getType());
                                    if(tuiJianList==null || tuiJianList.size()==0){
                                        ToastShow("暂无更多数据");
                                    }else{
                                        datas.addAll(tuiJianList);
                                        adapter.notifyDataSetChanged();
                                    }
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                });
    }

    private void addAction() {
        adapter = new TuiJianAdapter(this);
        adapter.setType(1);
        adapter.setList(datas);
        lv_data.setAdapter(adapter);
    }

    private void initListener() {
        lv_data.setXListViewListener(this);
        lv_data.setPullLoadEnable(true);
        lv_data.setPullRefreshEnable(true);
        lv_data.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if(GloData.getCustomerDTO()==null){
                    Intent loginIntent = new Intent(FastDetailActivity.this,LoginActivity.class);
                    startActivity(loginIntent);
                    return;
                }
                if(position==0){
                    return;
                }
                TuiJian action = (TuiJian) adapter.getItem(position-1);
                if(action==null){
                    return;
                }
                Intent intent = new Intent(FastDetailActivity.this, WebViewActivity.class);
                intent.putExtra("name",action.name);
                intent.putExtra("url",action.url);
                intent.putExtra("id",action.id);
                intent.putExtra("type",1);
                startActivity(intent);
            }
        });
    }

    private void initView() {
        lv_data = (XListView) findViewById(R.id.lv_data);
    }

    @Override
    public void onRefresh() {
        datas = new ArrayList<>();
        pageNo = 1;
        loadData(fastBean.id);
    }

    @Override
    public void onLoadMore() {
        pageNo ++;
        loadData(fastBean.id);
    }
}
